package edu.friday.repository;

import edu.friday.common.result.RestResult;
import edu.friday.model.SysRole;
import edu.friday.repository.custom.SysRoleCustomRepository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;

@Repository
public interface SysRoleRepository extends JpaRepository<SysRole,Long> , SysRoleCustomRepository {
    @Query(value = "select r.role_id from sys_role as r " +
            "inner join sys_user_role as ur on ur.role_id = r.role_id " +
                "inner join sys_user as u on u.user_id = ur.user_id " +
                    "WHERE r.del_flag='0' and u.user_id=:userId",nativeQuery = true)
    List<Long> selectRoleIdsByUserId(@Param("userId") Long userId);

    @Transactional
    @Modifying
    @Query(value = "update sys_role set status=:status where role_id=:roleId")
    int saveStatus(@Param("roleId")Long roleId,@Param("status")String status);

    @Query(value = "select r.role_id from sys_role as r " +
            "inner join sys_user_role as ur on ur.role_id = r.role_id " +
            "inner join sys_user as u on u.user_id = ur.user_id " +
            "WHERE r.del_flag='0' and ur.user_id=:userId",nativeQuery = true)
    List<SysRole> selectRoleByUserId(@Param("userId") Long userId);
}
